package org.jeecgframework.web.service.impl;

import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.web.service.CbsCustomizeReportServiceI;
import org.jeecgframework.web.system.pojo.base.CbsCustomizeReportEntity;
import org.jeecgframework.web.system.pojo.base.CbsRotaLevelEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.io.Serializable;

@Service("cbsCustomizeReportService")
@Transactional
public class CbsCustomizeReportServiceImpl extends CommonServiceImpl implements CbsCustomizeReportServiceI {


    public <T> void delete(T entity) {
        super.delete(entity);
        //执行删除操作配置的sql增强
        this.doDelSql((CbsCustomizeReportEntity) entity);
    }

    public <T> Serializable save(T entity) {
        Serializable t = super.save(entity);
        //执行新增操作配置的sql增强
        this.doAddSql((CbsCustomizeReportEntity) entity);
        return t;
    }

    public <T> void saveOrUpdate(T entity) {
        super.saveOrUpdate(entity);
        //执行更新操作配置的sql增强
        this.doUpdateSql((CbsCustomizeReportEntity) entity);
    }

    /**
     * 默认按钮-sql增强-新增操作
     *
     * @param id
     * @return
     */
    public boolean doAddSql(CbsCustomizeReportEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-更新操作
     *
     * @param id
     * @return
     */
    public boolean doUpdateSql(CbsCustomizeReportEntity t) {
        return true;
    }

    /**
     * 默认按钮-sql增强-删除操作
     *
     * @param id
     * @return
     */
    public boolean doDelSql(CbsCustomizeReportEntity t) {
        return true;
    }

    /**
     * 替换sql中的变量
     *
     * @param sql
     * @return
     */
    public String replaceVal(String sql, CbsCustomizeReportEntity t) {
        sql = sql.replace("#{id}", String.valueOf(t.getId()));
        sql = sql.replace("#{field_chinese}", String.valueOf(t.getFieldChinese()));
        sql = sql.replace("#{numberic_field}", String.valueOf(t.getNumbericField()));
        sql = sql.replace("#{field_type}", String.valueOf(t.getFieldType()));
        sql = sql.replace("#{term}", String.valueOf(t.getTerm()));
        sql = sql.replace("#{rota_name}", String.valueOf(t.getRotaName()));
        sql = sql.replace("#{is_group}", String.valueOf(t.getIsGroup()));
        sql = sql.replace("#{is_zd}", String.valueOf(t.getIsZd()));
        sql = sql.replace("#{UUID}", UUID.randomUUID().toString());
        return sql;
    }

    @Override
    public List<CbsCustomizeReportEntity> getTimeDiff(List<String> list) {
        List<CbsCustomizeReportEntity> resultList = new ArrayList<CbsCustomizeReportEntity>();
        List<CbsCustomizeReportEntity> lists = this.findByProperty(CbsCustomizeReportEntity.class, "timeDiff", "1");
        for (CbsCustomizeReportEntity cbsCustomizeReportEntity : lists) {
            if (list.contains(cbsCustomizeReportEntity.getRotaName())) {
                resultList.add(cbsCustomizeReportEntity);
            }
        }
        return resultList;
    }
}